package com.ruoyi.file.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.file.service.ISysFileService;
import com.ruoyi.system.api.domain.SysFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * 文件请求处理
 * 
 * @author ruoyi
 */
@RestController
public class SysFileController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file,Integer authId)
    {
        try
        {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file, authId);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setDisplaySize(FileUtils.getFileSize(file.getSize()));
            sysFile.setUrl(url);
            sysFile.setDownloadUrl(sysFileService.getDownloadUrl(url));
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    @GetMapping("download")
    public R<byte[]> download(String bucketName, String fileName, HttpServletResponse response)
    {
        try
        {
            AjaxResult ajax = AjaxResult.success();
            // 上传并返回访问地址
//            String bucketName = "image";
//            String fileName = "lims基础信息数据库表设计图.png";
            byte[] fileData = sysFileService.downloadFile(bucketName,fileName,response);
            if(fileData == null){
                return R.fail("未找到文件！");
            }
            return R.ok(fileData);
        }
        catch (Exception e)
        {
            log.error("下载文件失败", e);
            return R.fail(e.getMessage());
        }
    }

//    @GetMapping("download")
//    public AjaxResult download(String bucketName,String fileName,HttpServletResponse response)
//    {
//        try
//        {
//            AjaxResult ajax = AjaxResult.success();
//            // 上传并返回访问地址
////            String bucketName = "image";
////            String fileName = "lims基础信息数据库表设计图.png";
//            String url = sysFileService.downloadFile(bucketName,fileName,response);
//            ajax.put("fileName", fileName);
//            ajax.put("bucketName", bucketName);
//            return ajax;
//        }
//        catch (Exception e)
//        {
//            log.error("下载文件失败", e);
//            return AjaxResult.error(e.getMessage());
//        }
//    }
}